package com.zcl.demo.concurrent;

import java.util.concurrent.Semaphore;

/**
 * @author 周成龙
 * @date 2021-01-28 15:46
 * description
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(0);
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000L);
                    System.out.println("执行任务1");
                    semaphore.release();

                }catch (Exception e){
                    System.out.println(e.toString());
                }

            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000L);
                    System.out.println("执行任务2");
                    semaphore.release();
                }catch (Exception e){
                    System.out.println(e.toString());
                }

            }
        }.start();
        try {
            semaphore.acquire(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行main线程");
    }
}
